package com.huangd.platform.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huangd.platform.modules.sys.dao.TblConfigDao;
import com.huangd.platform.modules.sys.entity.TblConfigEntity;
import com.huangd.platform.common.utils.PageUtils;
import com.huangd.platform.common.utils.Query;
import com.huangd.platform.datasource.annotation.DataSource;
import com.huangd.platform.modules.sys.service.TblConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("tblConfigService")
@DataSource("platform")
public class TblConfigServiceImpl extends ServiceImpl<TblConfigDao, TblConfigEntity> implements TblConfigService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TblConfigEntity> page = this.page(
                new Query<TblConfigEntity>().getPage(params),
                new QueryWrapper<TblConfigEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void update(TblConfigEntity configEntity) {
        this.updateById(configEntity);
    }

    @Override
    public String getByCode(String code) {

        String result = baseMapper.getByCode(code);
        return result == null ? "" : result;
    }

    @Override
    public List<String> getUpgradeServiceList() {

        List<String> list = new ArrayList<>();
        String code = getByCode("service.upgrade");

        if (StringUtils.isNotBlank(code)) {
            String[] array = code.split(",");

            for (String service : array) {
                service = service.trim();
                if (service.isEmpty())
                    continue;

                list.add(service);
            }
        }
        return list;
    }

    @Override
    public List<String> getWebServiceList() {

        List<String> list = new ArrayList<>();

        String code = getByCode("service.web");
        if (StringUtils.isNotBlank(code)) {
            String[] array = code.split(",");

            for (String service : array) {
                service = service.trim();
                if (service.isEmpty())
                    continue;

                list.add(service);
            }
        }

        return list;
    }

    @Override
    public List<String> getRechargeServiceList() {

        List<String> list = new ArrayList<>();

        String code =  getByCode("service.recharge");
        if (StringUtils.isNotBlank(code)) {
            String[] array = code.split(",");

            for (String service : array) {
                service = service.trim();
                if (service.isEmpty())
                    continue;

                list.add(service);
            }
        }

        return list;
    }

    @Override
    public String getRechargeService() {
        List<String> list = getRechargeServiceList();
        if (list == null || list.size() == 0)
            return null;

        return list.get(0);
    }
}